package ae.pcfc.cldos.online.common.manager.provider;

import java.util.List;

import org.springframework.scheduling.annotation.Async;

import ae.pcfc.cldos.online.common.clddb.domain.manager.AppMetaDataDOM;
import ae.pcfc.cldos.online.common.exception.RequestAllreadyExistException;
import ae.pcfc.cldos.online.common.manager.BaseManager;
import ae.pcfc.cldos.online.common.manager.GsInfoManager;
import ae.pcfc.cldos.online.gs.ConstantsGS;
import ae.pcfc.cldos.online.gs.service.client.GovtSysGS;
import ae.pcfc.cldos.online.gs.service.client.vo.BankGuaranteeOperationVO;
import ae.pcfc.cldos.online.gs.service.client.vo.GSFinancialDataVO;
import ae.pcfc.cldos.online.gs.service.client.vo.StepCommentsVO;
import ae.pcfc.cldos.online.gs.service.client.vo.WarningCommentsVO;

/**
 * 
 * @author arosa
 *
 */
public class GsInfoManagerImpl extends BaseManager implements GsInfoManager {

	private enum RemarksBG{
		DEBIT_REMARK("ReturnedToFNP"), CREDIT_REMARK("TakenFromFNP");
		
		private String remarkText;
		
		private RemarksBG(String remarkText){
			this.remarkText = remarkText;
		}
		
		public String getRemarkText() {
			return remarkText;
		}
	}
	
	private enum Actions{
		CREDIT, DEBIT;
	}
	
	private GovtSysGS govtSysGS;
	private AppMetaDataDOM appMetaDataDOM;
	
	
	public AppMetaDataDOM getAppMetaDataDOM() {
		return appMetaDataDOM;
	}

	public void setAppMetaDataDOM(AppMetaDataDOM appMetaDataDOM) {
		this.appMetaDataDOM = appMetaDataDOM;
	}

	public void setGovtSysGS(GovtSysGS govtSysGS) {
		this.govtSysGS = govtSysGS;
	}
	
	/* (non-Javadoc)
	 * @see ae.pcfc.cldos.online.common.manager.GsInfoManager#retriveFinancialData(java.lang.String, java.lang.Long)
	 */
	public GSFinancialDataVO retriveFinancialData(String coLicNo,Long clientId){
		GSFinancialDataVO gsFinantialVO = govtSysGS.retreiveFinancialData(coLicNo);
//		if(gsFinantialVO.getBgPaid() - gsFinantialVO.getBgRequired() < 0){
//			gsFinantialVO.setBankGuaranteeBalance(0);
//		}else{
			gsFinantialVO.setBankGuaranteeBalance(gsFinantialVO.getBgPaid() - gsFinantialVO.getBgRequired());
//		}
		return gsFinantialVO;
	}
	
	public String getNextOfflineAppId(String processCode){
		return govtSysGS.getNextOfflineAppId(processCode);
	}

	public void setClientMedicalType(String coLicNo,String medicalType){
		govtSysGS.setClientMedicalType(coLicNo, medicalType);		
	}
	
	public String getClientMedicalType(String coLicNo){
		return govtSysGS.getClientMedicalType(coLicNo);		
	}
	
	public boolean cancelGSAppRequest(String offlineAppId){
		return govtSysGS.cancelAppRequest(offlineAppId);
	}
	public boolean rejectAppRequest(String offlineAppId){
		return govtSysGS.rejectAppRequest(offlineAppId);
	}
	
	public String getWfStatus(String clientRefNo){
		return govtSysGS.getWfStatus(clientRefNo);
	}
	
	public void addVisitVisaBG(String companyLicenseNo, String initiatedBy){
		BankGuaranteeOperationVO bankGuaranteeOperationVO = new BankGuaranteeOperationVO();
		bankGuaranteeOperationVO.setCompanyLicenseNo(companyLicenseNo);
		bankGuaranteeOperationVO.setBgAmount(ConstantsGS.__BG_VISIT_VISA);
		bankGuaranteeOperationVO.setInitiatedBy(initiatedBy);
		bankGuaranteeOperationVO.setRemark(RemarksBG.CREDIT_REMARK.getRemarkText());
		bankGuaranteeOperationVO.setRequestType(Actions.CREDIT.toString());
		govtSysGS.bankGuaranteeOperation(bankGuaranteeOperationVO);
	}
	
	public void removeVisitVisaBG(String companyLicenseNo, String initiatedBy){
		BankGuaranteeOperationVO bankGuaranteeOperationVO = new BankGuaranteeOperationVO();
		bankGuaranteeOperationVO.setCompanyLicenseNo(companyLicenseNo);
		bankGuaranteeOperationVO.setBgAmount(ConstantsGS.__BG_VISIT_VISA);
		bankGuaranteeOperationVO.setInitiatedBy(initiatedBy);
		bankGuaranteeOperationVO.setRemark(RemarksBG.DEBIT_REMARK.getRemarkText());
		bankGuaranteeOperationVO.setRequestType(Actions.DEBIT.toString());
		govtSysGS.bankGuaranteeOperation(bankGuaranteeOperationVO);
	}
	
	public void addEmpVisaBG(String companyLicenseNo, String initiatedBy){
		BankGuaranteeOperationVO bankGuaranteeOperationVO = new BankGuaranteeOperationVO();
		bankGuaranteeOperationVO.setCompanyLicenseNo(companyLicenseNo);
		bankGuaranteeOperationVO.setBgAmount(ConstantsGS.__BG_EMP_VISA);
		bankGuaranteeOperationVO.setInitiatedBy(initiatedBy);
		bankGuaranteeOperationVO.setRemark(RemarksBG.CREDIT_REMARK.getRemarkText());
		bankGuaranteeOperationVO.setRequestType(Actions.CREDIT.toString());
		govtSysGS.bankGuaranteeOperation(bankGuaranteeOperationVO);
	}
	
	public void removeEmpVisaBG(String companyLicenseNo, String initiatedBy){
		BankGuaranteeOperationVO bankGuaranteeOperationVO = new BankGuaranteeOperationVO();
		bankGuaranteeOperationVO.setCompanyLicenseNo(companyLicenseNo);
		bankGuaranteeOperationVO.setBgAmount(ConstantsGS.__BG_EMP_VISA);
		bankGuaranteeOperationVO.setInitiatedBy(initiatedBy);
		bankGuaranteeOperationVO.setRemark(RemarksBG.DEBIT_REMARK.getRemarkText());
		bankGuaranteeOperationVO.setRequestType(Actions.DEBIT.toString());
		govtSysGS.bankGuaranteeOperation(bankGuaranteeOperationVO);
	}
	
	@Async
	public void setIncidentId(String offlineAppId, String incidentId){
		govtSysGS.setIncidentId(offlineAppId, incidentId);
	}
	
	/* (non-Javadoc)
	 * @see ae.pcfc.cldos.online.common.manager.GsInfoManager#updateAppReqWorkFlowStatus(java.lang.String, java.lang.String)
	 */
	public void updateAppReqWorkFlowStatus(String offlineAppId,String wfStatus){
		govtSysGS.updateAppReqWorkFlowStatus(offlineAppId, wfStatus);
	}
	/* (non-Javadoc)
	 * @see ae.pcfc.cldos.online.common.manager.GsInfoManager#insertStepComments(ae.pcfc.cldos.online.gs.service.client.vo.StepCommentsVO)
	 */
	public void insertStepComments(StepCommentsVO stepCommentsVO){
		govtSysGS.insertStepComments(stepCommentsVO);
	}
	
	/* (non-Javadoc)
	 * @see ae.pcfc.cldos.online.common.manager.GsInfoManager#getWarningRemarks(java.lang.String)
	 */
	public List<WarningCommentsVO>  getWarningRemarks(String licNo){
		return govtSysGS.getWarningRemarks(licNo);
	}
	/* (non-Javadoc)
	 * @see ae.pcfc.cldos.online.common.manager.GsInfoManager#isRequestExist(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void isRequestExist(String processCode,String empId, String visitorId) throws RequestAllreadyExistException{
		boolean flag = govtSysGS.isRequestExist(processCode, empId, visitorId);
		if(flag){
			throw new RequestAllreadyExistException();
		}
	}
}
